import Vue from "vue";
import { api } from "@/api/api.js";

function fomatTime(value) {
    var formatTime = function(fmt) {
        var o = {
            "M+": this.getMonth() + 1, //月份
            "d+": this.getDate(), //日
            "h+": this.getHours(), //小时
            "m+": this.getMinutes(), //分
            "s+": this.getSeconds(), //秒
            "q+": Math.floor((this.getMonth() + 3) / 3), //季度
            S: this.getMilliseconds() //毫秒
        };
        if (/(y+)/.test(fmt))
            fmt = fmt.replace(
                RegExp.$1,
                (this.getFullYear() + "").substr(4 - RegExp.$1.length)
            );
        for (var k in o)
            if (new RegExp("(" + k + ")").test(fmt))
                fmt = fmt.replace(
                    RegExp.$1,
                    RegExp.$1.length == 1
                        ? o[k]
                        : ("00" + o[k]).substr(("" + o[k]).length)
                );
        return fmt;
    };
    return value
        ? formatTime.call(new Date(value), "yyyy-MM-dd hh:mm:ss")
        : "暂无";
}
// 0: 未付款；1: 待发货；2: 已发货；3: 已完成；4: 退款中；5: 退款成功；6：已取消）
function stateSwitchFliter(val) {
    switch (val) {
        case 0:
            return "未付款";
        case 1:
            return "待发货";
        case 2:
            return "已发货";
        case 3:
            return "已完成";

        case 4:
            return "退款中";
        case 5:
            return "退款成功";
        case 6:
            return "已取消";
        default:
            return val;
    }
}
// 提现状态：0: 申请中；1: 申请中: 申请中；3：提现失败
function drawMoneyFliter(val) {
    switch (val) {
        case 0:
            return "申请中";
        case 1:
            return "申请中";
        case 2:
            return "已提现";
        case 3:
            return "提现失败";
        default:
            return val;
    }
}
function userStateSwitchFliter(val) {
    if (val === 0) {
        return "正常";
    } else {
        return "已禁用";
    }
}
function userIsWhiteFliter(val) {
    if (val === 0) {
        return "未加入";
    } else {
        return "已加入";
    }
}

function signOut() {
    Vue.prototype.http
        .post(api.logOut, {})
        .then(res => {
            if (res.data.code == 200) {
                this.$message({
                    type: "success",
                    message: "登出成功",
                    center: true
                });
                this.$cookie.delete("token");
                this.$router.replace({
                    name: "login"
                    // query: { redirect: router.currentRoute.fullPath }
                });
            } else {
                // this.$message({
                //   type: 'error',
                //   message: '登出失败',
                //   center: true
                // })
            }
        })
        .catch(res => {
            this.$message({
                message: "后台接口错误，请重试",
                type: "error",
                center: true
            });
        });
}
function getPrice(val) {
    return val / 100;
}
export default {
    fomatTime,
    stateSwitchFliter,
    userStateSwitchFliter,
    userIsWhiteFliter,
    drawMoneyFliter,
    signOut,
    getPrice
};
